class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int cost = prices[0], profit = 0;
        for (auto price : prices)
        {
            cost = min(cost, price);
            profit = max(profit, price - cost);
        }
        return profit;
    }
};